import { GetNav, GetInfoList, GetHotList, GetNewList, ReadConfig } from "@/services";
import arrayToTree from "array-to-tree"
export default {
  state: {
    headNav: [],
    list: {
      count: 0,
      arr: [],
    },
    hotList: [],
    newList: [],
    typeList: [],
    // banner: [
    //   {
    //     "src":
    //       "/oldDATA/pics/lunbo/23.jpg",
    //     // obj.src,
    //     "href": "/", "isEnable": true, "orderNum": "0", "index": 0
    //   }
    // ],
    // banner: window.iverhome["banner"].sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable),
    // newsitems: window.iverhome["newsitems"].sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable),
    // ability: window.iverhome["ability"].sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable),
    // imgsider: window.iverhome["imgsider"].sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable),
    // calendarsider: window.iverhome["calendarsider"].sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable),
  },
  subscriptions: {
    // setup({ dispatch, history }) {
    //   // dispatch({ type: "fetchNewList", payload: { limit: 7 } })
    //   // dispatch({ type: "fetchHome", payload: { limit: 7 } })
    // },
  },
  reducers: {
    setNav(state, { arr }) {
      state.headNav = arr;
    },
    setList(state, { list }) {
      state.list = list;
    },
    setHotList(state, { arr }) {
      state.hotList = arr;
    },
    setNewList(state, { arr }) {
      state.newList = arr;
    },
    setTypeList(state, { arr }) {
      state.typeList = arr;
    },
    setBanner(state, { arr }) {
      state.banner = arr;
    },
    setNewsitems(state, { arr }) {
      state.newsitems = arr;
    },
    setAbility(state, { arr }) {
      state.ability = arr;
    },
    setImgsider(state, { arr }) {
      state.imgsider = arr;
    },
    setCalendarsider(state, { arr }) {
      state.calendarsider = arr;
    },
  },
  effects: {
    *fetchNav({ type, payload }, { put, call, select }) {
      if (window.iverinfotype != undefined) {
        const arr = window.iverinfotype;
        yield put({ "type": "setTypeList", arr })
        const dealArr = arrayToTree(arr.map(item => ({
          ...item,
          id: item.id.toString(),
          name: item.displayName,
          fid: item.fid.toString(),
          type: item.type
        })), {
          rootID: "0",
          parentProperty: "fid",
          customID: "id",
          childrenProperty: "children"
        });
        yield put({ "type": "setNav", arr: dealArr })
      }
      else {
        const response = yield call(GetNav);
        const { result, success } = response;
        if (success) {
          const { arr } = result;
          yield put({ "type": "setTypeList", arr })
          const dealArr = arrayToTree(arr.map(item => ({
            ...item,
            id: item.id.toString(),
            name: item.displayName,
            fid: item.fid.toString(),
            type: item.type
          })), {
            rootID: "0",
            parentProperty: "fid",
            customID: "id",
            childrenProperty: "children"
          });
          yield put({ "type": "setNav", arr: dealArr })
        }
      }
    },
    *fetchList({ type, payload }, { put, call, select }) {
      yield put({
        "type": "setList", list: {
          count: 0,
          arr: [],
        }
      })
      const response = yield call(GetInfoList, payload);
      const { result, success } = response;
      if (success) {
        yield put({ "type": "setList", list: result })
      }
    },
    *fetchHotList({ type, payload }, { put, call, select }) {
      const response = yield call(GetHotList, payload);
      const { result, success } = response;
      if (success) {
        yield put({ "type": "setHotList", arr: result.arr })
      }
    },
    *fetchNewList({ type, payload }, { put, call, select }) {
      const response = yield call(GetNewList, payload);
      const { result, success } = response;
      if (success) {
        yield put({ "type": "setNewList", arr: result.arr })
      }
    },
    *fetchHome({ type, payload }, { put, call, select }) {
      // let banner = yield call(ReadConfig, { name: "banner" });
      // if (banner.success) {
      //   const arr = JSON.parse(banner.result).sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable);
      //   yield put({ "type": "setBanner", arr })
      // }
      // let newsitems = yield call(ReadConfig, { name: "newsitems" });
      // if (newsitems.success) {
      //   const arr = JSON.parse(newsitems.result).sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable);
      //   yield put({ "type": "setNewsitems", arr })
      // }
      // let ability = yield call(ReadConfig, { name: "ability" });
      // if (ability.success) {
      //   if (ability.success) {
      //     const arr = JSON.parse(ability.result).sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable);
      //     yield put({ "type": "setAbility", arr })
      //   }
      // }
      // let imgsider = yield call(ReadConfig, { name: "imgsider" });
      // if (imgsider.success) {
      //   const arr = JSON.parse(imgsider.result).sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable);
      //   yield put({ "type": "setImgsider", arr })
      // }
      // let calendarsider = yield call(ReadConfig, { name: "calendarsider" });
      // if (calendarsider.success) {
      //   const arr = JSON.parse(calendarsider.result).sort((a, b) => (Number(b.orderNum) - Number(a.orderNum))).filter(item => item.isEnable);
      //   yield put({ "type": "setCalendarsider", arr })
      // }

    },
  },
}
